﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Caching;

namespace Backend.JYD.Cache
{
    internal class HttpCache : ICache
    {
        public HttpCache()
        {
        }

        public void DeleteCacheItem(string cacheKey)
        {
            HttpRuntime.Cache.Remove(cacheKey);
        }

        public T GetCachedItem<T>(string cacheKey)
        {
            try
            {
                return (T)HttpRuntime.Cache.Get(cacheKey);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void SetCachedItem<T>(string cacheKey, T cacheObject)
        {
            HttpRuntime.Cache.Add(cacheKey, cacheObject, null, DateTime.Now.AddMinutes(CacheMgr.Instance.Config.CacheTime), System.Web.Caching.Cache.NoSlidingExpiration, CacheItemPriority.High, null);
        }

        public void ClearCache()
        {
            var list = HttpRuntime.Cache.GetEnumerator();
            while (list.MoveNext())
            {
                if (list.Key != null)
                    this.DeleteCacheItem(list.Key.ToString());
            }
        }
    }
}
